package org.uni_hi.lsep.gui;

import java.awt.BorderLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;

import org.lobobrowser.html.gui.HtmlPanel;
import org.uni_hi.lsep.AppManager;

public class MainFrame {

	public enum ContentPanelType{TEST,TEXT,RESULT};
	
	private static final long serialVersionUID = 1L;
	
	private JPanel jContentPane = null;
	private JScrollPane indexScrollPane = null;
	private JTree indexTree = null;
	private JSplitPane mainSplitPane = null;
	private MenuBar mainJMenuBar = null;
	private StatusPanel statusPanel = null;
	private JPanel contentScrollPane = null;
	private TestPanel testPanel = null;

	private JPanel rightComponentPanel;

	private NavigationbarPanel navigationbar;

	private HtmlPanel htmlPanel=null;

	private JFrame _frame=null; 
	
	/**
	 * This is the default constructor
	 */
	public MainFrame() {
		init();
		getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		getFrame().addWindowListener(new WindowListener() {
			
			@Override
			public void windowOpened(WindowEvent e) {}
			
			@Override
			public void windowIconified(WindowEvent e) {}
			
			@Override
			public void windowDeiconified(WindowEvent e) {}
			
			@Override
			public void windowDeactivated(WindowEvent e) {}
			
			@Override
			public void windowClosing(WindowEvent e) {}
			
			@Override
			public void windowClosed(WindowEvent e) {}
			
			@Override
			public void windowActivated(WindowEvent e) {
				//Splashscreen bei Start anzeigen mit "data/Bilder/Start.jpg"
				Thread splashThread=new Thread(new SplashScreen(3000L,getJContentPane()));
				splashThread.start();
				getFrame().removeWindowListener(this);
			}
		});
	}
	
	public JFrame getFrame(){
		if(_frame==null){
			_frame=new JFrame();
		}
		return _frame;
	}
	
	/**
	 * This method initializes mainJMenuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	public MenuBar getMainMenuBar() {
		if (mainJMenuBar == null) {
			mainJMenuBar = new MenuBar();
		}
		return mainJMenuBar;
	}
	
	public void setContentPanelType(ContentPanelType cp){
		if(getContentScrollPane().getComponentCount()>0){
			getContentScrollPane().remove(0);
		}
		getMainMenuBar().setLoadSaveEnabled(false);
		if(ContentPanelType.TEST.equals(cp)){
			getContentScrollPane().add(getTestPanel());
		}else if(ContentPanelType.TEXT.equals(cp)){
			getContentScrollPane().add(getHtmlPanel());
		}else if(ContentPanelType.RESULT.equals(cp)){
			getContentScrollPane().add(getTestPanel());
			getTestPanel().addResultPanel(null);
		}
		getContentScrollPane().repaint();
	}

	/**
	 * This method initializes statusPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private StatusPanel getStatusPanel() {
		if (statusPanel == null) {
			statusPanel = new StatusPanel();
		}
		return statusPanel;
	}

	/**
	 * This method initializes quizScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	public JPanel getContentScrollPane() {
		if (contentScrollPane == null) {
			contentScrollPane = new JPanel();
			contentScrollPane.setLayout(new BoxLayout(contentScrollPane, BoxLayout.PAGE_AXIS));
			contentScrollPane.add(getHtmlPanel());
		}
		return contentScrollPane;
	}

	
	/**
	 * This method initializes quizPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	public TestPanel getTestPanel() {
		if (testPanel == null) {
			testPanel = new TestPanel();
		}
		return testPanel;
	}

	/**
	 * This method initializes jTextPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	public HtmlPanel getHtmlPanel() {
		if (htmlPanel == null) {
			htmlPanel = new HtmlPanel();
			
			AppManager.getBrowser().setHTMLPanel(htmlPanel);
			htmlPanel.setPreferredWidth(800);
		}
		return htmlPanel;
	}

	public void setStatus(String status) {
		getStatusPanel().setStatus(status);
	}

	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	protected void init() {
		getFrame().setSize(1200, 870);
		getFrame().setJMenuBar(getMainMenuBar());
		getFrame().setContentPane(getJContentPane());
		getFrame().setTitle("Firewall-Lernsoftware");
		getFrame().setIconImages(AppManager.getImageIcons());
	}
	


	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getMainSplitPane(), BorderLayout.CENTER);
			jContentPane.add(getStatusPanel(), BorderLayout.PAGE_END);
		}
		return jContentPane;
	}

	/**
	 * This method initializes indexScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getIndexScrollPane() {
		if (indexScrollPane == null) {
			indexScrollPane = new JScrollPane();
			indexScrollPane.setViewportView(getIndexTree());
		}
		return indexScrollPane;
	}

	/**
	 * This method initializes indexTree	
	 * 	
	 * @return javax.swing.JTree	
	 */
	public JTree getIndexTree() {
		if (indexTree == null) {
			try{
			 indexTree = new NavigationTree();
			}catch(NullPointerException e){
				JOptionPane.showMessageDialog(null, "Dateien konnten nicht geladen werden!", "Fehler", JOptionPane.ERROR_MESSAGE);
			}
		}
		return indexTree;
	}

	/**
	 * This method initializes mainSplitPane	
	 * 	
	 * @return javax.swing.JSplitPane	
	 */
	private JSplitPane getMainSplitPane() {
		if (mainSplitPane == null) {
			mainSplitPane = new JSplitPane();
			mainSplitPane.setName("mainSplitPane");
			mainSplitPane.setRightComponent(getRightComponentPanel());
			mainSplitPane.setLeftComponent(getIndexScrollPane());
			mainSplitPane.setDividerLocation(200);
			mainSplitPane.setDividerSize(5);
		}
		return mainSplitPane;
	}
	
	private JPanel getRightComponentPanel(){
		if(rightComponentPanel==null){
			rightComponentPanel=new JPanel();
			rightComponentPanel.setLayout(new BorderLayout());
			rightComponentPanel.add(getNavigationbar(),BorderLayout.PAGE_START);
			rightComponentPanel.add(getContentScrollPane(), BorderLayout.CENTER);
		}
		return rightComponentPanel;
	}
	
	public NavigationbarPanel getNavigationbar(){
		if(navigationbar==null){
			navigationbar=new NavigationbarPanel();
		}
		return navigationbar;
	}
}  //  @jve:decl-index=0:visual-constraint="10,51"
